<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>C++: CanlibTutorial, VS2017</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_tutorial_cpp_canlib_vs2017.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">C++: CanlibTutorial, VS2017 </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#section_tutorial_cpp_canlib">Setting up a project to use CANlib</a><ul><li class="level2"><a href="#section_tutorial_cpp_canlib_project">Creating an empty project</a></li>
<li class="level2"><a href="#section_tutorial_cpp_add_canlib">Adding CANlib</a></li>
</ul>
</li>
<li class="level1"><a href="#section_tutorial_cpp_code">Adding the code</a></li>
</ul>
</div>
<div class="textblock"><p>This tutorial will take you through setting up a Visual Studio 2017 project and creating an application that will be able to list connected devices.</p>
<h1><a class="anchor" id="section_tutorial_cpp_canlib"></a>
Setting up a project to use CANlib</h1>
<p>In this section we will enable the usage of Kvaser CANlib in Visual Studio 2017 when creating a C++ project which can be used to build both WIN32 and WIN64 applications. We will also handle the platform settings: x86 and x64.</p>
<p>First we must have the "Kvaser Drivers for Windows" and "Kvaser CANlib SDK" installed, see <a class="el" href="section_install_windows.html">Installing on Windows</a> for details on how to download and install these.</p>
<p>Now we are ready to start Visual Studio 2017 (C++) and create our project (I am using "Microsoft Visual Studio Professional 2017").</p>
<h2><a class="anchor" id="section_tutorial_cpp_canlib_project"></a>
Creating an empty project</h2>
<p>After starting Visual Studio 2017 (C++), we create a new project by selecting "File–&gt;New-&gt;Project...". Choose to create a "Windows Console Application, Visual C++", name it "CanlibTutorial" (the Solution will now also be named "CanlibTutorial"), and press "OK".</p>
<div class="image">
<img src="cpp_tutorial_1_create_project.png" alt="cpp_tutorial_1_create_project.png" width="600px"/>
<div class="caption">
Creating the CanlibTutorial project</div></div>
<p>You will now be given an empty main function that does nothing.</p>
<div class="image">
<img src="cpp_tutorial_2_empty_project.png" alt="cpp_tutorial_2_empty_project.png" width="600px"/>
<div class="caption">
The CanlibTutorial project</div></div>
<h2><a class="anchor" id="section_tutorial_cpp_add_canlib"></a>
Adding CANlib</h2>
<p>Now we need to do five things to add CANlib so that we can build the project:</p>
<ol type="1">
<li>The LIB name "canlib32.lib" to Additional Dependencies</li>
<li>Add path to INC (...\Kvaser\Canlib\INC\<a class="el" href="canlib_8h.html" title="Definitions for the CANlib API. ">canlib.h</a>)</li>
<li>Add path to LIB (x86) (...\Kvaser\Canlib\Lib\MS\canlib32.lib)</li>
<li>Add path to LIB (x64) (...\Kvaser\Canlib\Lib\x64\canlib32.lib)</li>
<li>Add <code>#include "canlib.h"</code></li>
</ol>
<p>We start by opening "Project-&gt;CanlibTutorial Properties..." and in the drop down for "Configuration" we select "All Configurations".</p>
<div class="image">
<img src="cpp_tutorial_3_linker_input_configuration.png" alt="cpp_tutorial_3_linker_input_configuration.png"/>
<div class="caption">
Use the same configuration for all configurations.</div></div>
<p> We also do the same for the next drop down selector "Platform" where we select "All Platforms". This might trigger a pop-up informing us that "There are no property pages for the selection".</p>
<div class="image">
<img src="cpp_tutorial_4_linker_input_no_property.png" alt="cpp_tutorial_4_linker_input_no_property.png"/>
<div class="caption">
VS2017 closes the configuration by giving some information</div></div>
<p> Press OK and reopen the properties once again using "Project-&gt;CanlibTutorial Properties...", in the left pane select "Linker" and then "Input". Now click in the text field to the right of "Additional Dependencies", this will reveal a drop down menu which we select, revealing an &lt;Edit...&gt; text which we click on.</p>
<div class="image">
<img src="cpp_tutorial_5_linker_additional_dependencies.png" alt="cpp_tutorial_5_linker_additional_dependencies.png"/>
<div class="caption">
Click the &lt;Edit...&gt; text to start editing the values</div></div>
<p> This opens up the "Additional Dependencies" where we add "canlib32.lib" in the top text box and press OK. </p><dl class="section note"><dt>Note</dt><dd>This applies to all libraries in the canlibSDK (<a class="el" href="page_canlib.html">CAN bus API (CANlib)</a>, <a class="el" href="page_linlib.html">LIN bus API (LINlib)</a>, <a class="el" href="page_kvadblib.html">Database API (kvaDbLib)</a>, <a class="el" href="page_kvmlib.html">Memorator API (kvmlib)</a>, <a class="el" href="page_kvamemolibxml.html">Memorator XML API (kvaMemoLibXML)</a>, <a class="el" href="page_kvlclib.html">Converter API (kvlclib)</a>, <a class="el" href="page_kvrlib.html">Remote Device API (kvrlib)</a>) which can be included by adding their files to the dependency list.</dd></dl>
<div class="image">
<img src="cpp_tutorial_6_linker_additional_dependencies_editor.png" alt="cpp_tutorial_6_linker_additional_dependencies_editor.png"/>
<div class="caption">
Adding canlib32.lib to the dependencies.</div></div>
<p> When the pop-up has been closed, we can now verify that "canlib32.lib" has been added to the "Additional Dependencies" field.</p>
<p>The next step is to add the path to <a class="el" href="canlib_8h.html" title="Definitions for the CANlib API. ">canlib.h</a>. On my machine I installed Kvaser CANlib SDK to "D:\CanlibSDK\CanlibSDK_5.26" (default is "C:\Program Files (x86)\Kvaser\Canlib"), which means that "canlib.h" can be found in "D:\CanlibSDK\CanlibSDK_5.26\INC" (default would be "C:\Program Files (x86)\Kvaser\Canlib\INC").</p>
<p>Once again open up the Property pages (using "Project-&gt;CanlibTutorial Properties...") and in the left pane select "C/C++" and "General". Click in the value field for "Additional Include Directories" and add the path to "canlib.h", in my case "D:\\CanlibSDK\\CanlibSDK_5.26". Now press Apply.</p>
<div class="image">
<img src="cpp_tutorial_7_cpp_general_additional_include.png" alt="cpp_tutorial_7_cpp_general_additional_include.png"/>
<div class="caption">
Add path to canlib.h to compiler include directories</div></div>
<p> From the "Platform" drop down selector, select "Win32", which once again closes our window with the pop-up informing us that "There are no property pages for the selection". Press OK and reopen the properties once again using "Project-&gt;CanlibTutorial Properties...", in the left pane select "Linker" and "General". Click in the value field for "Additional Library Directories" and add the path to the 32-bit version of "canlib32.lib", in my case "D:\CanlibSDK\CanlibSDK_5.26\Lib\MS" (default is "C:\Program Files (x86)\Kvaser\Canlib\Lib\MS"). Now press Apply.</p>
<p>We repeat the process for the 64 bit version by selecting "x64" as the "Platform" and enter (after reopening the CANlibTutorial properties) "D:\CanlibSDK\CanlibSDK_5.26\Lib\x64" (default is "C:\Program Files (x86)\Kvaser\Canlib\Lib\x64") in the field for "Additional Library Directories". Press OK to save and close the properties.</p>
<div class="image">
<img src="cpp_tutorial_8_linker_general_additional_library.png" alt="cpp_tutorial_8_linker_general_additional_library.png"/>
<div class="caption">
Adding library dependancies to 64-bit platform</div></div>
<h1><a class="anchor" id="section_tutorial_cpp_code"></a>
Adding the code</h1>
<p>The last step is to add <code>#include "canlib.h"</code> to CanlibTutorial.cpp. We do this as well as enter some more code...</p>
<div class="fragment"><div class="line"><span class="comment">//-----------------------------------------------------------</span></div><div class="line"><span class="comment">// This is a sample program for the Visual Studio 2017 CANlib tutorial.</span></div><div class="line"><span class="comment">// It prints a list of connected CAN interfaces.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// For further information please refer to the tutorial section of the CANlib documentation.</span></div><div class="line"><span class="comment">//-----------------------------------------------------------</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;stdafx.h&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="canlib_8h.html">canlib.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;iostream&quot;</span></div><div class="line"></div><div class="line"><span class="comment">// When called, CheckForError will check for and print any error.</span></div><div class="line"><span class="comment">// The program is aborted if an error has occured.</span></div><div class="line"><span class="keywordtype">void</span> CheckForError(<span class="keywordtype">char</span> cmd[50], <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">        <span class="comment">//if stat not ok, print error</span></div><div class="line">        <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>)</div><div class="line">        {</div><div class="line">                <span class="keywordtype">char</span> buf[255];</div><div class="line">                buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">                <a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line"></div><div class="line">                printf(<span class="stringliteral">&quot;[%s] %s: failed, stat=%d\n&quot;</span>, cmd, buf, (<span class="keywordtype">int</span>)stat);</div><div class="line">                printf(<span class="stringliteral">&quot;ABORT (Press any key)&quot;</span>);</div><div class="line"></div><div class="line">                getchar();</div><div class="line">                abort();</div><div class="line">        }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// ListChannels prints a list of all connected CAN interfaces.</span></div><div class="line"><span class="keywordtype">void</span> ListChannels()</div><div class="line">{</div><div class="line">        <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">        <span class="keywordtype">int</span> number_of_channels;</div><div class="line"></div><div class="line">        <span class="keywordtype">int</span> device_channel;</div><div class="line">        <span class="keywordtype">char</span> device_name[255];</div><div class="line"></div><div class="line">        <span class="comment">// Get number of channels</span></div><div class="line">        stat = <a class="code" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels</a>(&amp;number_of_channels);</div><div class="line">        CheckForError((<span class="keywordtype">char</span>*)<span class="stringliteral">&quot;canGetNumberOfChannels&quot;</span>, stat);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (number_of_channels &gt; 0) {</div><div class="line">                printf(<span class="stringliteral">&quot;%s %d %s\n&quot;</span>, <span class="stringliteral">&quot;Found&quot;</span>, number_of_channels, <span class="stringliteral">&quot;channels&quot;</span>);</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> {</div><div class="line">                printf(<span class="stringliteral">&quot;Could not find any CAN interface.\n&quot;</span>);</div><div class="line">        }</div><div class="line"></div><div class="line">    <span class="comment">// Loop and print all channels</span></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; number_of_channels; i++) {</div><div class="line">                stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(i, <a class="code" href="canlib_8h.html#afc7edc82e3d370444c3b83c66a9c6479">canCHANNELDATA_DEVDESCR_ASCII</a>, device_name, <span class="keyword">sizeof</span>(device_name));</div><div class="line">                CheckForError((<span class="keywordtype">char</span>*)<span class="stringliteral">&quot;canGetChannelData&quot;</span>, stat);</div><div class="line"></div><div class="line">                stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(i, <a class="code" href="canlib_8h.html#a4e0823a649d26191cde50f2c4fa5a65d">canCHANNELDATA_CHAN_NO_ON_CARD</a>, &amp;device_channel, <span class="keyword">sizeof</span>(device_channel));</div><div class="line">                CheckForError((<span class="keywordtype">char</span>*)<span class="stringliteral">&quot;canGetChannelData&quot;</span>, stat);</div><div class="line"></div><div class="line">                printf(<span class="stringliteral">&quot;%s %d %s %d\n&quot;</span>, <span class="stringliteral">&quot;Found channel:&quot;</span>, i, device_name, (device_channel + 1));</div><div class="line">        }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">        <span class="comment">//canStatus stat;</span></div><div class="line"></div><div class="line">        <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">        printf(<span class="stringliteral">&quot;%s\n&quot;</span>, <span class="stringliteral">&quot;CanlibTutorial started&quot;</span>);</div><div class="line"></div><div class="line">        ListChannels();</div><div class="line">        </div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><p>To run the code, select platform from the drop-down menu, and press the green play icon.</p>
<div class="image">
<img src="cpp_tutorial_9_select_platform.png" alt="cpp_tutorial_9_select_platform.png"/>
<div class="caption">
Select platform to run, and then press the play icon</div></div>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_tutorial.html">Tutorials</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
